#!/bin/bash
#
# Copyright 2015-2017 Adrian DC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# === Android Device Targets ===
function androiddevicestarget()
{
  # Usage: androiddevicestarget [boot/system/...] [device] (Devices targets mapper)

  # Variables
  local request=${1};
  local device=${2};
  local fullpath;
  local platform=;
  local partition=;
  local target_devices='dora kagura kugo suzu huashan hayabusa mint tsubasa shuang';

  # Ignore unsupported devices
  if [[ ! "${target_devices}" == *"${device}"* ]]; then
    device=;
  fi;

  # Autodetect
  if [ -z "${device}" ]; then
    device=${TARGET_PRODUCT#*_};
  fi;
  if [ -z "${device}" ]; then
    device=${KBUILD_DIFFCONFIG%_*};
  fi;
  if [ -z "${device}" ]; then
    device=${ANDROID_DEVICE_TARGET};
  fi;
  if [ -z "${device}" ]; then
    fullpath=$(pwd);
    for devicename in ${target_devices}; do
      if [[ "${fullpath}" == *"${devicename}"* ]]; then
        device=${devicename};
      fi;
    done;
  fi;
  if [ -z "${device}" ]; then
    >&2 echo '';
    >&2 echo ' export TARGET_PRODUCT=device_name is empty';
    >&2 echo '';
    >&2 echo -n " > Device target [${target_devices}] : ";
    read -r device;
    >&2 echo '';
    export ANDROID_DEVICE_TARGET=${device};
  fi;

  # Platform
  case "${device}" in
    dora|kagura|kugo|suzu)
      platform=/dev/block/bootdevice/by-name;;
    huashan|hayabusa|mint|tsubasa|shuang|*)
      platform=/dev/block/platform/msm_sdcc.1/by-name;;
  esac;

  # Input request
  case "${request}" in

    # Device codename
    codename)
      echo "${device}";
      return;;

    # Boot partition
    boot)
      case "${device}" in
        huashan|hayabusa|mint|tsubasa)
          partition=Kernel;;
        dora|f8131|shuang|*)
          partition=boot;;
      esac;;

    # FOTAKernel partition
    fota)
      partition=FOTAKernel;;

    # OEM partition
    oem)
      partition=oem;;

    # Recovery partition
    recovery)
      case "${device}" in
        dora|f8131)
          partition=recovery;;
        huashan|hayabusa|mint|tsubasa|shuang|*)
          partition=FOTAKernel;;
      esac;;

    # System partition
    system)
      case "${device}" in
        huashan|hayabusa|mint|tsubasa)
          partition=System;;
        dora|f8131|shuang|*)
          partition=system;;
      esac;;

    # Unknown partition
    *)
      partition=unknown;;
  esac;

  # Result
  echo "${platform}/${partition}";
}

# === Phone Codename to Target ===
function codenametotarget()
{
  # Usage
  if [ -z "${1}" ]; then
    echo '';
    echo ' Usage: codenametotarget <codename> (Codename to build target)';
    echo '';
    return;
  fi;

  # Filter devices from input
  case "${1}" in
    dora)
      echo 'f8131';;
    kagura)
      echo 'f8331';;
    *)
      echo "${1}";;
  esac;
}

# === Repo Get Device ===
function repogetdevice()
{
  # Usage: repogetdevice (Detect device name from repo environment)

  # Variables
  local cwd;
  local product=${TARGET_PRODUCT#*_};

  # Repo root
  cwd=$(pwd);
  croot;

  # Check device
  if ! ls './device/'*"/${product}" 1> /dev/null 2>&1; then

    # Analyse devices
    while IFS= read -r -d '' device_path; do
      if grep -q "${TARGET_PRODUCT}" "${device_path}"; then
        product=$(dirname "${device_path}");
        product=$(basename "${product}");
      fi;
    done < <(find ./device/ -name cm.mk -or -name lineage.mk -or -name vendorsetup.sh);

  fi;

  # Result
  echo "${product}";
  cd "${cwd}";
}
